<template>
  <div class="work-table">
    <el-card class="search-card" v-show="showSearch">
      <el-form @submit.native.prevent :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="事件名称" prop="eventName" label-width="80px">
          <el-input v-model="queryParams.eventName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="安全分类" prop="riskLevel" label-width="80px">
          <el-select class="select-box" :popper-append-to-body="false" v-model="queryParams.majorCode" placeholder="请选择" filterable clearable @keyup.enter.native="handleQuery">
            <el-option v-for="item in eventCategorysData" :key="item.majorCode" :label="item.majorName" :value="item.majorCode"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="风险等级" prop="riskLevel" label-width="80px">
          <el-select class="select-box" :popper-append-to-body="false" v-model="queryParams.riskLevel" placeholder="请选择" clearable @keyup.enter.native="handleQuery">
            <el-option v-for="item in riskLevelData" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否已导出" prop="exported" label-width="90px">
          <el-select class="select-box" :popper-append-to-body="false" v-model="queryParams.exported" placeholder="请选择" clearable @keyup.enter.native="handleQuery">
            <el-option v-for="item in exportedData" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="事件时间" label-width="80px">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          ></el-date-picker>
        </el-form-item>
        <div style="float: right">
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('search') }}</el-button>
          <el-button class="button-ty" icon="el-icon-refresh" @click="resetQuery">{{ $t('reset') }}</el-button>
        </div>
      </el-form>
    </el-card>

    <el-card class="table-card" style="border-radius: 8px">
      <el-row :gutter="10" style="margin-bottom: 16px">
        <el-col :span="1.5">
          <el-button plain icon="el-icon-download" size="small" @click="handleExport">导出工单</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
      </el-row>
      <div class="data-table">
        <el-table v-loading="loading" :data="tableData" :row-key="getRowKey" :border="false" ref="multipleTable" :max-height="495">
          <el-table-column prop="majorCodeName" label="安全分类" align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="eventName" label="事件名称" align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="风险等级" align="center" prop="riskLevel" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-tag :type="getRiskType(scope.row.riskLevel)">{{ scope.row.riskLevel }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="channelName" label="摄像头名称" align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="snapshotUrl" label="快照" align="center">
            <template slot-scope="scope">
              <el-image :src="scope.row.snapshotUrl" :preview-src-list="getBigSnap(scope.row.snapshotUrl)" fit="cover" style="width: 28px; height: 28px">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="事件状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.event_confirmed" :value="scope.row.confirmed" />
            </template>
          </el-table-column>
          <el-table-column prop="eventTime" label="事件时间" align="center" :show-overflow-tooltip="true"></el-table-column>
<!--          <el-table-column prop="exported" label="是否已导出" align="center"></el-table-column>-->
          <el-table-column label="是否已导出" align="center">
            <!-- 使用 template 插槽自定义显示内容 -->
            <template slot-scope="scope">
              <!-- 三元表达式：如果 exported 等于1，显示“是”，否则显示“否” -->
              {{ scope.row.exported == 1 ? '是' : '否' }}
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true"></el-table-column>
          <!--          <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width"-->
          <!--                           width="180">-->
          <!--            <template v-slot="scope">-->
          <!--              <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改-->
          <!--              </el-button>-->
          <!--              <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除-->
          <!--              </el-button>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
        </el-table>

        <pagination style="margin-bottom: 20px" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </div>
    </el-card>

    <!-- 导出工单 -->
    <el-dialog class="form-dialog" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" :autosize="{ minRows: 3, maxRows: 5 }" style="width: 400px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确认</el-button>
        <el-button @click="cancel">{{ $t('cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getEventList } from '@/api/private/aiMonitoring';
import { getEventCategorys } from '@/api/private/configurationManagement';
export default {
  name: "workTable",
  dicts: ['event_confirmed'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        tenantId: 1,
        pageNum: 1,
        pageSize: 10,
        confirmed: 1,
        eventName: null,
        riskLevel: null,
        majorCode: null,
        exported: null,
      },
      eventCategorysData: [],
      riskLevelData: [{
        value: 'A',
        label: 'A'
      }, {
        value: 'B',
        label: 'B'
      }, {
        value: 'C',
        label: 'C'
      }],
      exportedData: [{
        value: '1',
        label: '是'
      }, {
        value: '0',
        label: '否'
      }],
      dateRange: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      tableData: [],
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: '',
      rules: {},
    }
  },
  created() {
    this.getList();
    this.getEventCategorysData();
  },
  methods: {
    // 风险等级映射到预设类型
    getRiskType(riskLevel) {
      switch (riskLevel) {
        case "A":
          return "danger";
        case "B":
          return "warning";
        case "C":
          return "success";
        default:
          return "default"; // 默认类型（灰色）
      }
    },
    // 查询列表
    getList() {
      this.loading = true;
      getEventList(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.loading = false;
    },
    // 查询事件分类下拉列表
    getEventCategorysData() {
      const data = {
        tenantId: 1,
      }
      getEventCategorys(data).then((response) => {
        this.eventCategorysData = response.data;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        remark: undefined,
        exportData: true,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm('queryForm');
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.reset();
      this.open = true;
      this.title = "导出工单";
    },
    /** 导出按钮确认操作 */
    async submitForm() {
      try {
        await this.download(
          'event/export',
          {
            ...this.queryParams,
            ...this.form
          },
          `工单报表${new Date().getTime()}.xlsx`
        );
        this.open = false;
        this.getList();
      } catch (error) {
        console.error('导出失败', error);
        this.open = false;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getRowKey(row){
      return row.id;
    },
    getBigSnap: function (snapshotUrl) {
      return [snapshotUrl];
    },
  }
}
</script>

<style lang="scss" scoped>
.search-card {
  background: rgba(0,11,61,0.8);
  color: #fff;
  border: none;
  margin-bottom: 15px;
  padding: 3px 0;
  overflow: visible !important;

  ::v-deep .el-input__inner {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(22, 93, 255, 0.5);
    color: #fff; /* 文字颜色 */
  }
  ::v-deep .el-range-editor--medium .el-range-input {
    background: none;
    color: #fff; /* 文字颜色 */
  }

  .select-box {
    // 穿透修改Element UI选择框的内部样式（可选）
    ::v-deep .el-input__inner {
      background: rgba(255, 255, 255, 0.1);
      border-color: rgba(22, 93, 255, 0.5);
      color: #fff; /* 文字颜色 */
      font-size: px2font(14);
      padding: 0 px2vw(30) 0 px2vw(15);
    }
    ::v-deep .el-select-dropdown__item:hover {
      background-color: rgba(22, 93, 255, 0.9);
      color: #fff; // 可选： hover 时文字变白，增强对比度
    }
    ::v-deep .el-select-dropdown__item.selected {
      background-color: rgba(22, 93, 255, 0.7);
      color: #fff;
    }
    ::v-deep .el-select-dropdown__item {
      color: white;
    }
    ::v-deep .el-select-dropdown {
      background: rgba(0,11,61, 0.9);
      border-image: linear-gradient(
          180deg,
          rgba(22, 93, 255, 0),
          rgba(22, 93, 255, 0.59),
          rgba(22, 93, 255, 1)
      ) 1 1;
    }
  }
}

.button-ty {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(22, 93, 255, 0.5);
  color: #fff; /* 文字颜色 */
}
::v-deep .top-right-btn .custom-button[data-v-38ed449c] {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(22, 93, 255, 0.5);
  color: #fff; /* 文字颜色 */
}
.table-card {
  background: rgba(0,11,61,0.8);
  border: none;
}
.data-table {
  ::v-deep .el-table {
    border: none !important;
  }

  /* 2. 表头底下线条 */
  ::v-deep .el-table th.el-table__cell.is-leaf {
    border-bottom: none;
  }

  /* 2. 表头背景（列标题区域） */
  ::v-deep .el-table tr {
    background: rgba(22,93,255);
  }
  /* 2. 表头字体大小（列标题区域） */
  ::v-deep .el-table th.el-table__cell>.cell {
    //font-size: px2font(16);
    //border-right: none !important; /* 移除单元格右侧边框 */
    //border-left: none !important; /* 移除单元格左侧边框（通常默认无，但保险起见） */
  }

  ::v-deep .el-button--text {
    color: #ffffff;
  }

  /* 3. 表格体背景（数据行区域） */
  /* 3.1 普通行背景 */
  ::v-deep .el-table__body-wrapper,
  ::v-deep .el-table__row {
    background: rgba(0,11,61,0.79) !important; /* 普通行背景（示例：白色） */
  }
  /* 3.1 普通行字体大小 */
  ::v-deep .el-table td.el-table__cell div {
    //font-size: px2font(14);
  }
  /* 表格体单元格背景（确保与行背景一致） */
  ::v-deep .el-table__cell {
    //background-color: inherit !important; /* 继承行背景 */
    background: rgba(0,11,61,0.79) !important; /* 表格体文字颜色（可选） */
    border-right: none !important; /* 移除单元格右侧边框 */
    border-left: none !important; /* 移除单元格左侧边框（通常默认无，但保险起见） */
    border: 1px solid rgba(255,255,255,0.1);
    color: rgba(255,255,255,0.9);
  }

  /* 4. 行 hover 状态背景（鼠标悬停时） */
  ::v-deep .el-table__row:hover {
    background: rgba(22,93,255,0.4) !important; /* hover 背景（示例：淡蓝） */
  }

  /* 5. 表格边框颜色（可选：与背景色协调） */
  ::v-deep .el-table__border,
  ::v-deep .el-table-cell {
    background: rgba(0,11,61,0.79) !important; /* 边框颜色（示例：浅灰） */
  }

  ::v-deep .el-table--border::after, .el-table--group::after {
    width: 0 !important;
  }

  ::v-deep .el-table::before {
    height: 0 !important;
  }


  /* 1. 分页组件整体文字颜色（基础设置） */
  ::v-deep .el-pagination {
    color: #ffffff !important; /* 默认文字颜色（示例：深灰色） */
  }

  ::v-deep input.el-input__inner {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(22, 93, 255, 0.5);
    color: #ffffff;
  }

  ::v-deep button.btn-prev {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(22, 93, 255, 0.5);
    color: #ffffff;
  }

  ::v-deep button.btn-next {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(22, 93, 255, 0.5);
    color: #ffffff;
  }

  /* 2. 页码按钮文字颜色 */
  ::v-deep .el-pager li {
    color: #ffffff !important; /* 普通页码文字 */
    background: rgba(255, 255, 255, 0.1) !important;
    border-color: rgba(22, 93, 255, 0.5) !important;
  }

  /* 3. 当前选中页码的文字颜色 */
  ::v-deep .el-pager li.active {
    background-color: #409EFF !important;
    color: #ffffff !important; /* 选中页码文字（通常与背景色对比） */
  }

  /* 4. 上一页/下一页按钮文字颜色 */
  ::v-deep .el-pagination__prev,
  ::v-deep .el-pagination__next {
    color: #ffffff !important; /* 箭头按钮文字 */
  }

  /* 5. 禁用状态（如第一页的"上一页"）的文字颜色 */
  ::v-deep .el-pagination__prev.is-disabled,
  ::v-deep .el-pagination__next.is-disabled {
    color: #ffffff !important; /* 禁用状态文字（示例：浅灰色） */
  }

  /* 6. 总条数/跳转文本的颜色（如"共 100 条"） */
  ::v-deep .el-pagination__total,
  ::v-deep .el-pagination__jump {
    color: #ffffff !important; /* 说明文字颜色 */
  }

  ::v-deep .pagination-container {
    background: none;
    margin-top: px2vh(10);
    margin-bottom: px2vh(20);
  }

  ::v-deep .el-input--medium .el-input__inner {
    height: 28px;
  }
}
//::v-deep .el-input__inner {
//  background: rgba(255, 255, 255, 0.1);
//  border-color: rgba(22, 93, 255, 0.5);
//  color: #fff; /* 文字颜色 */
//}
::v-deep .el-dialog {
  margin-top: 8vh !important;
  background: rgba(0,11,61);
}
.form-dialog {
  ::v-deep .el-input__inner {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(22, 93, 255, 0.5);
    color: #fff; /* 文字颜色 */
  }
  ::v-deep .el-textarea__inner {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(22, 93, 255, 0.5);
    color: #ffffff;
  }
  ::v-deep .el-dialog__title {
    color: #ffffff;
  }
  ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #ffffff;
  }
  ::v-deep .el-form-item__label {
    color: #ffffff;
  }
}
</style>
